package zsj.androidknife.ThreadPool;

import android.os.AsyncTask;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2016/4/20 0020.
 */
public class ThreadPool {
    private static int THREAD_NUMBER = 3;

    private ThreadPool() {
    }

    private static class InstanceHolder {
        private static ExecutorService fixedExec = Executors.newFixedThreadPool(THREAD_NUMBER, new ThreadFactory()
        {
            private final AtomicInteger mCount = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r)
            {
                return new Thread(r, "AndroidKnife #" + mCount.getAndIncrement());
            }
        });
    }

    /**
     * 取得指定线程数的线程池
     * 但是如果之前调用过getThreadPool(),参数会失效
     * 只会返回一个默认有三个线程的FixedThreadPool
     * @param threadNumber 指定线程的数量
     * @return
     */
    public static ExecutorService getThreadPool(int threadNumber) {
        THREAD_NUMBER = threadNumber;
        return InstanceHolder.fixedExec;
    }

    public static ExecutorService getThreadPool() {
        return InstanceHolder.fixedExec;
    }


}
